package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class OrderTask {
    @Autowired
    OrderMapper orderMapper;
    //@Scheduled(cron = "0 * * * * *")
    public void OrderPayTimeOut() {
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-15);
        List<Orders> list=orderMapper.selectByOrderPayTimeOut(Orders.PENDING_PAYMENT,localDateTime);
        for (Orders orders : list) {
            orders.setStatus(Orders.CANCELLED);
            orders.setCancelReason("订单超时未支付，自动取消");
            orders.setCancelTime(LocalDateTime.now());
            orderMapper.update(orders);
        }
    }
    //@Scheduled(cron ="0 0 1 * * * ")
    public void OrderClean() {
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-60);
        List<Orders> list=orderMapper.selectByOrderPayTimeOut(Orders.DELIVERY_IN_PROGRESS,localDateTime);
        for (Orders orders : list) {
            orders.setStatus(Orders.COMPLETED);
            orderMapper.update(orders);
        }
    }
}
